package com.ryan.spring.annotation.config;

import com.ryan.spring.annotation.model.RainBow;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * Created by kaimin on 28/1/2019.
 * time : 09:13
 */
public class MyImportBeanDefinitionRegistor implements ImportBeanDefinitionRegistrar {
    /**
     *
     * @param importingClassMetadata 当前类的注解信息
     * @param registry BeanDefinition的注册类
     */
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        //手动向容器中注册bean

        boolean blue = registry.containsBeanDefinition("com.ryan.spring.annotation.model.Blue");
        boolean red = registry.containsBeanDefinition("com.ryan.spring.annotation.model.Red");
        if (blue && red) {
            //指定bean定义信息，（bean类型，名称等）
            RootBeanDefinition beanDefinition = new RootBeanDefinition(RainBow.class);
            //向容器中加入bean，并且指定bean名称
            registry.registerBeanDefinition("rainbow",beanDefinition);
        }


    }
}
